package com.igoal.web;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.tuckey.web.filters.urlrewrite.utils.StringUtils;

import com.igoal.dto.UserActivityLogDTO;
import com.igoal.utils.ConvertUtils;

@Controller
@RequestMapping("/ajax/activities.html")
public class AjaxActivityViewController extends BaseAjaxController {
	private final static Log logger = LogFactory.getLog(AjaxActivityViewController.class);
	
	@Override
	public String getViewPrefix() {
		return BaseControllerDelegate.AJAX_VIEW_PREFIX;
	}
	
	@RequestMapping(method = RequestMethod.GET)
	public String show(ModelMap modelMap, @RequestParam("lastOrderValue") String lastOrderValue,
			@RequestParam("username") String username, @RequestParam("exceptForMe") boolean isExceptForMe){
		List<UserActivityLogDTO> activities = new ArrayList<UserActivityLogDTO>();
		
		if(StringUtils.isBlank(username)){
			applyModel(modelMap, activities);
			return getViewPrefix() + "ajaxActivities";
		}
		
		try{
			long lastValue = ConvertUtils.toLong(lastOrderValue, -1l);
			Date lastItemUpdateDate = null;
			if(lastValue > 0l){
				lastItemUpdateDate = new Date(lastValue);
			}
			
			activities = actionLoggerService.getNewActivitiesLog(username, isExceptForMe, lastItemUpdateDate, false);			
		}catch (Exception e) {
			logger.error(e);
		}
		
		applyModel(modelMap, activities);
		return "ajax/ajaxActivities";
	}
}
